﻿using System;
using System.Collections.Generic;

namespace OestsCommon.Model
{
    /// <summary>
    /// 房间类
    /// </summary>
    public class Room
    {
        /// <summary>
        /// 房间成员
        /// </summary>
        [Serializable]
        public class RoomMember
        {
            /// <summary>
            /// 构造函数
            /// </summary>
            public RoomMember()
            {
                MemberId = 0;
                PostId = 0;
                Ready = false;
                SceneLoaded = false;
                IsFinish = false;
            }

            /// <summary>
            /// 构造函数
            /// </summary>
            public RoomMember(int mId)
            {
                MemberId = mId;
                PostId = 0;
                Ready = false;
                SceneLoaded = false;
                IsFinish = false;
            }

            /// <summary>
            /// 初始化成员和岗位
            /// </summary>
            /// <param name="mId"></param>
            /// <param name="mName"></param>
            /// <param name="postId"></param>
            public RoomMember(int mId, string mName, int postId)
            {
                MemberId = mId;
                MemberName = mName;
                PostId = postId;
                Ready = false;
                SceneLoaded = false;
                IsFinish = false;
            }

            /// <summary>
            /// 成员ID，用户id
            /// </summary>
            public int MemberId { get; set; }

            /// <summary>
            /// 成员名称
            /// </summary>
            public string MemberName { get; set; }

            /// <summary>
            /// 岗位ID
            /// </summary>
            public int PostId { get; set; }

            /// <summary>
            /// 准备好了，可以开始任务
            /// </summary>
            public bool Ready { get; set; }

            /// <summary>
            /// 加载完场景
            /// </summary>
            public bool SceneLoaded { get; set; }

            public bool IsFinish { get; set; }


            /// <summary>
            /// 设置岗位
            /// </summary>
            /// <param name="post"></param>
            public void SetPost(int post)
            {
                PostId = post;
            }

        }

        #region 房间对象属性

        /// <summary>
        /// 项目类型
        /// </summary>
        public ProjectType ProType = ProjectType.None;
        //  public ProjectType client;

        /// <summary>
        /// 房间id == 房主id
        /// </summary>
        public int RoomId { get; set; }

        /// <summary>
        /// 房间名称 单人时可为空
        /// </summary>
        public string RoomName { get; set; }

        /// <summary>
        /// 房间密码
        /// </summary>
        public string RoomPassword { get; set; }

        /// <summary>
        /// 任务
        /// </summary>
        public TaskModel Task { get; set; }

        /// <summary>
        /// 多人联机
        /// </summary>
        public bool MultiPlayer { get; set; }

        /// <summary>
        /// 是否在游戏中
        /// </summary>
        public bool InGame { get; set; }


        /// <summary>
        /// 无参
        /// </summary>
        public Room()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="proType"></param>
        /// <param name="roomId"></param>
        /// <param name="roomName"></param>
        /// <param name="roomPwd"></param>
        /// <param name="task"></param>
        public Room(ProjectType proType, int roomId, string roomName, string roomPwd, TaskModel task)
        {
            this.ProType = proType;
            this.RoomId = roomId;
            this.RoomName = roomName;
            this.RoomPassword = roomPwd;
            this.Task = task;
        }


        /// <summary>
        /// 成员信息
        /// </summary>
        public List<RoomMember> Members { get; set; }


        /// <summary>
        /// 用户加入房间并设置岗位
        /// </summary>
        /// <param name="std"></param>
        /// <param name="postId"></param>
        public void SetRole(Student std, int postId)
        {
            if (Members == null) Members = new List<RoomMember>();
            foreach (var item in Members)
            {
                if (item.MemberId == std.DBid)
                {
                    item.SetPost(postId);
                    return;
                }
            }

            //成员不在房间，加入房间并设置岗位
            RoomMember rm = new RoomMember(std.DBid, std.Name, postId);
            Members.Add(rm);
        }

        /// <summary>
        /// 设置岗位
        /// </summary>
        /// <param name="memberId"></param>
        /// <param name="postId"></param>
        /// <returns></returns>
        public bool SetPost(int memberId, int postId)
        {
            if (Members == null) Members = new List<RoomMember>();
            foreach (var item in Members)
            {
                if (item.MemberId == memberId)
                {
                    item.SetPost(postId);
                    return true;
                }
            }
            return false;
        }

        #endregion

        #region 房间对象方法
        /// <summary>
        /// 添加一个成员
        /// </summary>
        public void AddMember(int mId)
        {

            RoomMember rm = new RoomMember(mId);
            Members.Add(rm);
        }

        /// <summary>
        /// 删除一个成员
        /// </summary>
        public void DelMember(int mId)
        {
            if (Members == null) return;
            foreach (var item in Members)
            {
                if (item.MemberId == mId)
                {
                    Members.Remove(item);
                    break;
                }

            }
        }
        #endregion
    }
}
